package com.struts.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.struts2.views.jsp.IteratorStatus;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.struts.pojo.Person;

@SuppressWarnings("serial")
public class OgnlInteratorForMapStackAction extends ActionSupport{
	
	/**
	 * 测试struts2的iterator标签，循环输出valueStack值栈中list集合对象中的元素
	 * List<Person> list = new ArrayList<Person>()
	 */
	public String testIteratorTag_1() {
		List<Person> list = new ArrayList<Person>() ;
		list.add(new Person(1L,"张三01")) ;
		list.add(new Person(2L,"张三02")) ;
		list.add(new Person(3L,"张三03")) ;
		//将list集合添加到map栈中
		ActionContext.getContext().put("personList", list) ;
		//将list集合添加到对象栈中
		//直接添加到栈顶
//		ActionContext.getContext().getValueStack().push(list) ;
		//以key:value的形式添加到对象栈中
//		ActionContext.getContext().getValueStack().set("personList", list) ;
		return "iterator" ;
	}
	
	/**
	 * 测试struts2的iterator标签，循环输出valueStack值栈中map集合对象中的元素
	 * Map<String, Person> map = new HashMap<String, Person>()
	 */
	public String testIteratorTag_2() {
		Map<String, Person> map = new HashMap<String, Person>() ;
		map.put("person1", new Person(1L,"张三01")) ;
		map.put("person2", new Person(2L,"张三02")) ;
		map.put("person3", new Person(3L,"张三03")) ;
		ActionContext.getContext().put("personMap", map);
		return "iterator" ;
	}
	
	/**
	 * 测试struts2的iterator标签，循环输出valueStack值栈中数组对象中的元素
	 * Person[] persons = new Person[] {new Person(1L,"张三01"), new Person(1L,"张三02"), new Person(1L,"张三03")
	 */
	public String testIteratorTag_3() {
		Person[] persons = new Person[] {new Person(1L,"张三01"), new Person(1L,"张三02"), new Person(1L,"张三03")} ;
		ActionContext.getContext().put("personArray", persons) ;
		return "iterator" ;
	}
	
	/**
	 * 测试struts2的iterator标签，循环输出valueStack值栈中set集合对象中的元素
	 * Set<Person> set = new HashSet<Person>()
	 */
	public String testIteratorTag_4() {
		Set<Person> set = new HashSet<Person>() ;
		set.add(new Person(1L,"张三01")) ;
		set.add(new Person(2L,"张三02")) ;
		set.add(new Person(3L,"张三03")) ;
		ActionContext.getContext().put("personSet", set) ;
		return "iterator" ;
	}
	
	/**
	 * List<Map<String, Person>> list = new ArrayList<Map<String,Person>>()
	 */
	public String testIteratorTag_5() {
		Map<String, Person> map1 = new HashMap<String, Person>() ;
		map1.put("person1", new Person(1L,"张三01")) ;
		map1.put("person2", new Person(2L,"张三02")) ;
		map1.put("person3", new Person(3L,"张三03")) ;
		Map<String, Person> map2 = new HashMap<String, Person>() ;
		map2.put("person1", new Person(4L,"张三04")) ;
		map2.put("person2", new Person(5L,"张三05")) ;
		map2.put("person3", new Person(6L,"张三06")) ;
		List<Map<String, Person>> list = new ArrayList<Map<String,Person>>() ;
		list.add(map1) ;
		list.add(map2) ;
		ActionContext.getContext().put("personList", list) ;
		return "iterator" ;
	}
	
	/**
	 * Map<String, List<Person>> map = new HashMap<String, List<Person>>()
	 */
	public String testIteratorTag_6() {
		List<Person> list = new ArrayList<Person>() ;
		list.add(new Person(1L,"张三01")) ;
		list.add(new Person(2L,"张三02")) ;
		list.add(new Person(3L,"张三03")) ;
		Map<String, List<Person>> map = new HashMap<String, List<Person>>() ;
		map.put("map", list) ;
		ActionContext.getContext().put("personMap", map) ;
		return "iterator" ;
	}
	
	/**
	 * List<Map<String, List<Person>>> list2 = new ArrayList<Map<String,List<Person>>>()
	 */
	public String testIteratorTag_7() {
		List<Person> list = new ArrayList<Person>() ;
		list.add(new Person(1L,"张三01")) ;
		list.add(new Person(2L,"张三02")) ;
		list.add(new Person(3L,"张三03")) ;
		Map<String, List<Person>> map = new HashMap<String, List<Person>>() ;
		map.put("map", list) ;
		List<Map<String, List<Person>>> list2 = new ArrayList<Map<String,List<Person>>>() ;
		list2.add(map) ;
		ActionContext.getContext().put("personList", list2) ;
		return "iterator" ;
	}

}
